What is mmmagic?
The mmmagic npm package is a Node.js library used for detecting the MIME type and encoding of files. It is a binding to the libmagic library, which is used to identify file types based on their content rather than their file extension.
What are mmmagic's main functionalities?
Detect MIME type of a file
This feature allows you to detect the MIME type of a file. The code sample demonstrates how to use mmmagic to read a file and determine its MIME type, which is useful for handling files based on their content type.
const fs = require('fs');
const mmm = require('mmmagic'), Magic = mmm.Magic;
const magic = new Magic(mmm.MAGIC_MIME_TYPE);
magic.detectFile('path/to/file', function(err, result) {
if (err) throw err;
console.log(result); // e.g., 'image/jpeg'
});
Detect MIME encoding of a file
This feature allows you to detect the MIME encoding of a file. The code sample shows how to use mmmagic to determine the encoding of a file, which can be important for processing files correctly.
const fs = require('fs');
const mmm = require('mmmagic'), Magic = mmm.Magic;
const magic = new Magic(mmm.MAGIC_MIME_ENCODING);
magic.detectFile('path/to/file', function(err, result) {
if (err) throw err;
console.log(result); // e.g., 'binary'
});
Detect MIME type and encoding of a file
This feature allows you to detect both the MIME type and encoding of a file simultaneously. The code sample demonstrates how to use mmmagic to get both pieces of information in one call, which can be efficient for applications that need both details.
const fs = require('fs');
const mmm = require('mmmagic'), Magic = mmm.Magic;
const magic = new Magic(mmm.MAGIC_MIME);
magic.detectFile('path/to/file', function(err, result) {
if (err) throw err;
console.log(result); // e.g., 'image/jpeg; charset=binary'
});
Other packages similar to mmmagic
file-type
The file-type package is a popular alternative to mmmagic for detecting the MIME type of files. It is a pure JavaScript implementation and does not require native bindings, making it easier to install and use across different platforms. However, it may not support as many file types as mmmagic, which relies on the extensive libmagic database.
mime
The mime package is another alternative that provides a way to look up the MIME type based on file extension. Unlike mmmagic, it does not inspect the file content, so it is faster but less accurate for files with misleading extensions.
magic-bytes.js
magic-bytes.js is a library that detects file types based on magic numbers (specific byte sequences at the start of files). It is similar to mmmagic in that it inspects file content, but it is implemented in JavaScript and may not cover as many file types as mmmagic, which uses the comprehensive libmagic database.
Description
An async libmagic binding for node.js for detecting content types by data inspection.
Requirements
Install
npm install mmmagic
Examples
- Get general description of a file:
var Magic = require('mmmagic').Magic;
var magic = new Magic();
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err, result) {
if (err) throw err;
console.log(result);
});
- Get mime type for a file:
var mmm = require('mmmagic'),
Magic = mmm.Magic;
var magic = new Magic(mmm.MAGIC_MIME_TYPE);
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err, result) {
if (err) throw err;
console.log(result);
});
- Get mime type and mime encoding for a file:
var mmm = require('mmmagic'),
Magic = mmm.Magic;
var magic = new Magic(mmm.MAGIC_MIME_TYPE | mmm.MAGIC_MIME_ENCODING);
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err, result) {
if (err) throw err;
console.log(result);
});
- Get general description of the contents of a Buffer:
var Magic = require('mmmagic').Magic;
var magic = new Magic(),
buf = new Buffer('import Options\nfrom os import unlink, symlink');
magic.detect(buf, function(err, result) {
if (err) throw err;
console.log(result);
});
API
Magic methods
-
(constructor)([< mixed >magicSource][, < Integer >flags]) - Creates and returns a new Magic instance. magicSource
(if specified) can either be a path string that points to a (compatible) magic file to use or it can be a Buffer containing the contents of a (compatible) magic file. If magicSource
is not a string and not false
, the bundled magic file will be used. If magicSource
is false
, mmmagic will default to searching for a magic file to use (order of magic file searching: MAGIC
env var -> various file system paths (see man file
)). flags is a bitmask with the following valid values (available as constants on require('mmmagic')
):
- MAGIC_NONE - No flags set
- MAGIC_DEBUG - Turn on debugging
- MAGIC_SYMLINK - Follow symlinks (default for non-Windows)
- MAGIC_DEVICES - Look at the contents of devices
- MAGIC_MIME_TYPE - Return the MIME type
- MAGIC_CONTINUE - Return all matches (returned as an array of strings)
- MAGIC_CHECK - Print warnings to stderr
- MAGIC_PRESERVE_ATIME - Restore access time on exit
- MAGIC_RAW - Don't translate unprintable chars
- MAGIC_MIME_ENCODING - Return the MIME encoding
- MAGIC_MIME - (MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING)
- MAGIC_APPLE - Return the Apple creator and type
- MAGIC_NO_CHECK_TAR - Don't check for tar files
- MAGIC_NO_CHECK_SOFT - Don't check magic entries
- MAGIC_NO_CHECK_APPTYPE - Don't check application type
- MAGIC_NO_CHECK_ELF - Don't check for elf details
- MAGIC_NO_CHECK_TEXT - Don't check for text files
- MAGIC_NO_CHECK_CDF - Don't check for cdf files
- MAGIC_NO_CHECK_TOKENS - Don't check tokens
- MAGIC_NO_CHECK_ENCODING - Don't check text encodings
-
detectFile(< String >path, < Function >callback) - (void) - Inspects the file pointed at by path. The callback receives two arguments: an < Error > object in case of error (null otherwise), and a < String > containing the result of the inspection.
-
detect(< Buffer >data, < Function >callback) - (void) - Inspects the contents of data. The callback receives two arguments: an < Error > object in case of error (null otherwise), and a < String > containing the result of the inspection.